package com.hxci.web.controller;

import com.hxci.web.bean.Conn;

import javax.servlet.ServletConfig;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;

import static java.lang.System.out;

public class ReaderServlet extends HttpServlet {
    ServletConfig servletConfig;
    @Override
    public void init(ServletConfig config) throws ServletException {
        servletConfig=config;

    }
    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        super.doGet(req, resp);
    }

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
//        do_add(req,resp);
        String actionType = req.getParameter("actionType");
        out.println("readeType is :"+actionType);//        添加

        switch (actionType) {
            case "reader_update":
                do_update(req, resp);
                break;
            case "reader_add":
                do_add(req, resp);
                break;
            default:
                out.println("error:"+actionType);
                break;
        }
    }

    private void do_update(HttpServletRequest req, HttpServletResponse resp) throws ServletException,IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        ServletContext ctx=servletConfig.getServletContext();
        String realPath=ctx.getRealPath("/WEB-INF/db.properties");
        String readerid = req.getParameter("readerid");//借阅人id
        String readername = req.getParameter("readername");//借阅人姓名
        String sex = req.getParameter("sex");//性别
        String idcard = req.getParameter("idcard");//身份证号
        String xh = req.getParameter("xh");//学号

        String phone = req.getParameter("phone");//联系方式

        String sql = "UPDATE readers SET readername = ?, sex = ?, idcard = ?,xh = ?,phone = ? WHERE readerid = ?";
        List<Object> params = new ArrayList<>();
        params.add(readername);
        params.add(sex);
        params.add(idcard);
        params.add(xh);
        params.add(phone);
        params.add(readerid);
        Conn c=new Conn(realPath);
        c.doQuery(sql,params);
        resp.sendRedirect("reader.jsp");//        添加

    }

    protected void do_add(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        resp.setContentType("text/html;charset=utf-8");
        resp.setCharacterEncoding("utf-8");
        req.setCharacterEncoding("utf-8");
        ServletContext ctx=servletConfig.getServletContext();
        String realPath=ctx.getRealPath("/WEB-INF/db.properties");

        String readername = req.getParameter("readername");
        String sex = req.getParameter("sex");
        String idcard = req.getParameter("idcard");
        String xh = req.getParameter("xh");
        String phone = req.getParameter("phone");

        String sql = "INSERT INTO readers ( readername , sex, idcard,xh ,phone )" +
                "VALUES" +
                "( ?,?,?,?,?);";
        List<Object> params = new ArrayList<>();
        params.add(readername);
        params.add(sex);
        params.add(idcard);
        params.add(xh);
        params.add(phone);
        Conn c=new Conn(realPath);
        c.doQuery(sql,params);
        //在页面上显示输出成功
//        PrintWriter out = resp.getWriter();
//        out.println("注册成功");

        resp.sendRedirect("reader.jsp");
    }

}
